﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace FazaDemo.PL.Wpf
{
    public abstract class DataGridFactory
    {

        private Grid _parentGrid;

        protected DataGrid _workDgrid;

        protected WebApiClient _apiClient;

        public IDgridCustomizer Customizer { get; protected set; }

        public void Create(Grid parentGrid)
        {
            _parentGrid = parentGrid;
            AddGrid();
            AddColumns();
            LoadContent();
            SetPlaceInGrid();
        }

        public DataGrid Get()
        {
            _workDgrid = new DataGrid();
            _workDgrid.AutoGenerateColumns = false;
            AddColumns();
            LoadContent();
            return _workDgrid;
        }

        private void AddGrid()
        {
            _parentGrid.Children.Add(new DataGrid
            {
                AutoGenerateColumns = false,
                Margin = Customizer.DefThickness
            });
            UIElementCollection gridChildren = _parentGrid.Children;
            int gridChildCount = gridChildren.Count;
            _workDgrid = gridChildren[gridChildCount - 1] as DataGrid;
        }

        protected abstract void AddColumns();

        protected abstract void LoadContent();

        private void SetPlaceInGrid()
        {
            Customizer.SetGridPosition(_parentGrid);
        }

    }
}
